package cn.fsd.controller;

import cn.fsd.domain.Purchase_bill;
import cn.fsd.query.delivery.PurchaseBillQuery;
import cn.fsd.query.delivery.StoreroomCommodityQuery;
import cn.fsd.service.impl.PurchaseBillServiceImpl;
import cn.fsd.service.impl.StoreRoomCommodityServiceImpl;
import cn.fsd.util.Constants;
import cn.fsd.vo.storeroom.purchase.PurchaseBillCommodityVO;
import cn.fsd.vo.ResultBean;
import cn.fsd.vo.StoreRoomCommodityBean;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/purchaseBill")
public class PurchaseBillController {

  // TODO: 2023/7/3 此处需要改成调用库房相关接口
  @Autowired
  StoreRoomCommodityServiceImpl storeRoomCommodityService;
  @Autowired
  PurchaseBillServiceImpl purchaseBillService;


  @PostMapping
  @ApiOperation(value = "新建购货入库单")
  public ResultBean addNewPurchaseBill(@RequestBody Purchase_bill bill){
    StoreRoomCommodityBean commodity = (StoreRoomCommodityBean) storeRoomCommodityService.getCommodityById(bill.getCommodity_id(),bill.getStoreroom_id());
    if (commodity==null){
      return ResultBean.error("商品不存在");
    }
    if(StringUtils.isEmpty(bill.getCommodity_id())){
      return ResultBean.error("商品id不能为空");
    }
    if(StringUtils.isEmpty(bill.getCommodity_name())){
      return ResultBean.error("商品名称不能为空");
    }
    if(StringUtils.isEmpty(bill.getSupplier_id())){
      return ResultBean.error("供应商id不能为空");
    }
    if(StringUtils.isEmpty(bill.getStoreroom_id())){
      return ResultBean.error("库房id不能为空");
    }

    StoreroomCommodityQuery query = new StoreroomCommodityQuery();
    query.setCommodity_id(bill.getCommodity_id());
    query.setWarehouse_id(bill.getStoreroom_id());
    query.setPurchase_num(bill.getPurchase_num());
    // TODO: 2023/7/4 调用storeroom微服务接口
    if(storeRoomCommodityService.updatePurchase(query)==-1){
      return ResultBean.error("商品进货数量过多");
    }


    return ResultBean.success((Object)purchaseBillService.addNewPurchaseBill(bill));
  }

  @PostMapping("/queryBills")
  @ApiOperation(value = "查询购货入库单")
  public ResultBean queryBills(@RequestBody PurchaseBillQuery query){
      if(!Constants.SELECTBYSUPPLIER.equals(query.getSelectBy()) && !Constants.SELECTBYCODE.equals(query.getSelectBy())){
        return ResultBean.error("查询类型有误");
      }
      return ResultBean.success(purchaseBillService.queryBills(query));
  }

 @PostMapping("/returnGoods")
 @ApiOperation(value = "对进货单进行退货")
 public ResultBean returnGoods(@RequestBody Purchase_bill bill){
    if(StringUtils.isEmpty(bill.getBill_id())){
      return ResultBean.error("调拨单id不能为空");
    }
    if(StringUtils.isEmpty(bill.getStoreroom_id())){
      return ResultBean.error("库房id不能为空");
    }
    if(StringUtils.isEmpty(bill.getCommodity_id())){
      return ResultBean.error("商品id不能为空");
    }
    if(bill.getReturn_num()==null){
      return ResultBean.error("商品退回数量不能为空");
    }
    int res = purchaseBillService.returnGoods(bill);
    if(res==-1){
      return ResultBean.error("调拨单不存在");
    }
    if(res==-2){
      return ResultBean.error("退货数量过多或订单已经全部退货");
    }
    return ResultBean.success();
 }

  @PostMapping("/find")
  @ApiOperation(value = "(库房微服务调用)，查询购货单信息")
  public PageInfo<PurchaseBillCommodityVO> getPurchaseBillByPage(@RequestBody PurchaseBillQuery query){
    return purchaseBillService.getPurchaseBillByPage(query);
  }

  @PostMapping("/changeStatus")
  @ApiOperation(value = "String bill_id, Integer status")
  void changePurchase_status(@RequestBody Purchase_bill purchase_bill){
    purchaseBillService.changePurchase_status(purchase_bill);
  }
  @GetMapping("/getById")
  @ApiOperation(value = "（库房微服务调用）根据id获取购货单")
  Purchase_bill getPurchase_billById(String bill_id){
    return purchaseBillService.getPurchase_billById(bill_id);
  }
}
